﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TouchSocket.Core;
using TouchSocket.Sockets;

namespace TouchSocketClient
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            var tcpClient = new TcpClient();
            tcpClient.Connecting = (client, e) =>
            {
                client.SetDataHandlingAdapter(new FixedHeaderPackageAdapter() { FixedHeaderType = FixedHeaderType.Int });
            };//即将连接到服务器，此时已经创建socket，但是还未建立tcp
            tcpClient.Connected = (client, e) => { Console.WriteLine(client.IP); };//成功连接到服务器
            tcpClient.Disconnecting = (client, e) => { Console.WriteLine(client.IP); };//即将从服务器断开连接。此处仅主动断开才有效。
            tcpClient.Disconnected = (client, e) => { Console.WriteLine(client.IP); };//从服务器断开连接，当连接不成功时不会触发。
            tcpClient.Received = (client, e, r) =>
            {
                //从服务器收到信息。但是一般byteBlock和requestInfo会根据适配器呈现不同的值。
                var mes = Encoding.UTF8.GetString(e.Buffer, 0, e.Len);
                tcpClient.Logger.Info($"客户端接收到信息：{mes}");
                Console.WriteLine(mes);
            };



            //载入配置
            tcpClient.Setup(new TouchSocketConfig()
                .SetRemoteIPHost(new IPHost("192.168.1.2:55555"))
                .ConfigureContainer(a =>
                {
                    a.AddConsoleLogger();//添加一个日志注入
                }));

            tcpClient.Connect();//调用连接，当连接不成功时，会抛出异常。

            //Result result = tcpClient.TryConnect();//或者可以调用TryConnect
            //if (result.IsSuccess())
            //{

            //}

            tcpClient.Logger.Info("客户端成功连接");

            // tcpClient.Send("RRQM");


            while (true)
            {
                List<byte> res = new List<byte>();
                res.Add(1);
                using (Bitmap b = TakeScreenshot())
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        b.Save(ms, ImageFormat.Jpeg);

                        res.AddRange(ms.ToArray());

                        tcpClient.Send(res.ToArray());

                        //await Task.Delay(500);

                    }
                }


            }


            Console.ReadKey();
        }

        public static Bitmap TakeScreenshot()
        {
            // 获取屏幕的大小
            Rectangle bounds = Screen.GetBounds(Point.Empty);

            // 创建一个Bitmap对象并设置其大小
            Bitmap screenshot = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format32bppArgb);

            // 使用Graphics对象从屏幕上复制图像
            using (Graphics graphics = Graphics.FromImage(screenshot))
            {
                graphics.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
            }

            // 将图像保存到文件中
            //screenshot.Save(filename, ImageFormat.Png);
            return screenshot;
        }
    }

}

